<?php
/*
 * @class cruseremote_remote_create_user
 * @package create_user_remote
 * @framework AllStruck Plugin Framework
 * @author AllStruck
*/

// Need to load WordPress manually since this file is called directly by outside request, in a way...
require_once( '../../../../wp-load.php' );

if ($_POST['HandshakeKey'] === get_option('handshake_key')) {
	$cruser = new cruser_remote_create_user();
	$new_user_id = $cruser->create_new_user();
}

class cruseremote_remote_create_user extends create_user_remote {
	
	public function __construct() {
		$pfix = $this->short_object_prefix;
	}

	public function create_new_user() {
		$user_name = $_POST['Field1'];
		$first_name = $_POST['Field4'];
		$last_name = $_POST['Field5'];
		$email_address = $_POST['Field14'];
		$website_address = $_POST['Field12'];
		
		$user_id = username_exists( $_POST['Field1'] );
		if ( !$user_id ) {
			$random_password = wp_generate_password( $length=12, $include_standard_special_chars=false );
			$user_id = wp_insert_user( array('user_login'=>$user_name, 'user_pass'=>$random_password, 'user_email'=>$email_address, 'user_url'=>$website_address, 'first_name'=>$first_name, 'last_name'=>$last_name ));
			wp_new_user_notification($user_id, $random_password);
		} else {
			$random_password = __('User already exists.  Password inherited.');
		}
		return $user_id;
	}
}
$cruseremote_remote_create_user = new cruseremote_remote_create_user();
?>